<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\model\NewsCategoryModel;
use app\common\model\NewsModel;
use app\common\model\SettingModel;
use think\facade\Session;
use think\facade\Db;

    /**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 
	 */
class Setting extends Common
{
    public function myUpdate()
    {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $group = input('group');
        $data = $this->request->param();

        if(empty($group)){
            return json(['code'=>1, 'msg'=>'参数错误']);
        }
        if(empty($data)||!is_array($data)){
            return json(['code'=>1, 'msg'=>'参数错误']);
        }

        unset($data['group']);
        unset($data['file']);
        SettingModel::singleUpdate($data,$group,'add');

        return json(['code'=>0,'msg'=>'操作成功','data'=>$group]);
    }
    
    // 快捷管理网站是否开启灰色模式
    // public function editShu(){
    //     $admin_id = Session::get('admin_id');
    //     if(empty($admin_id)){
    //         return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
    //     }
    //     if ($this->request->isPost()) {
    //         $data = $this->request->Post();
    //         foreach ($data as $k=>$v){
    //             if($v == 'true'){
    //                 $v = 'on';
    //             }else if($v == 'false'){
    //                 $v = '';
    //             }
    //             $res =  Db::name('setting')->where('name',$k)->update(['value'=>$v]);
    //         }
    //         return json(['code'=>0,'msg'=>'操作成功']);
    //     } else {
    //         return json(['code'=>0,'msg'=>'操作错误，请刷新后重试']);
    //     }
    // }
    public function editShu() {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        if ($this->request->isPost()) {
            $value      = input('post.value');
            $category   = input('post.category');
            if($value == 'true'){
                $value = 'on';
            }elseif($value == 'false'){
                $value = 'off';
                
            }
            // 更新留言信息
            Db::name('setting')->where('name', $category)->update(['value' => $value]);
            return json(['code'=>0,'msg'=>'操作成功']);
        } else {
            return json(['code'=>0,'msg'=>'操作错误，请刷新后重试']);
        }
        $this->getSide();
        return $this->fetch();
    }

    public function getSettingList(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
    	$group = input('group');
        $map = [
        	['group','=',$group]
        ];
        $list = SettingModel::where($map)->column('value','name');
        return json(['code'=>0,'msg'=>'操作成功','data'=>$list]);
    }


    /**
     * 上传二维码
     */
    public function uploadQrcode(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');

        // 上传到本地服务器
        //$savename = \think\facade\Filesystem::putFile( 'topic', $file);
        $savename = \think\facade\Filesystem::disk('sti')->putFile( 'images', $file);

        $path = '/uploads/'.str_replace("\\","/",$savename);
        $fullpath = $this->request->scheme().'://'.$this->request->host().$path;

        $data['url'] = $path;
        $data['title'] = '图片';

        return json(['code'=>0, 'msg'=>'上传成功', 'data'=>$data, 'fullpath'=>$fullpath]);
    }


}
